/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChangeAnnotator.java
 *
 * Created on Oct 1, 2010, 8:13:13 PM
 */

package resultEditor.annotator;

import java.awt.Dimension;
import webservices.AssignmentsScreen;

/**
 *
 * @author Chris
 */
public class ChangeAnnotator extends javax.swing.JFrame {
    // variables
    private userInterface.GUI __gui;
    private String oldannotator_username, oldannotator_userid;
    private AssignmentsScreen assignmentScreen = null;

    /** Creates new form ChangeAnnotator */
    
    public ChangeAnnotator(userInterface.GUI _FATHER_WINDOW_HANDLE, AssignmentsScreen assignmentScreen) {
        __gui = _FATHER_WINDOW_HANDLE;
        this.assignmentScreen = assignmentScreen;
        
        // init and set dialog location
        initComponents();
        // set attributes of components before we display it
        setComponents();
        setLocation();

        // show current annotator's name
        updateScreen_for_CurrentAnnoator();
    }
    

    /**pre-process to components before really displaying the dialog*/
    private void setComponents(){
        // following codes are used to disable the function that ask user
        // allocate an id for the annotator
        jLabel2.setVisible(false);
        jTextField2.setText("999"); // no special means, just a default value
        jTextField2.setVisible(false);
        jCheckBox_apply_toRelated.setVisible(false);
        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jUserName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jPassword = new javax.swing.JPasswordField();
        jPanel6 = new javax.swing.JPanel();
        jCheckBox_apply_toAll = new javax.swing.JCheckBox();
        jCheckBox_apply_toRelated = new javax.swing.JCheckBox();
        jPanel8 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jID = new javax.swing.JLabel();

        jLabel2.setText("Current Annotator ID:");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Current Annotator:");
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(238, 238, 239));
        setResizable(false);
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
                formWindowLostFocus(evt);
            }
        });

        jPanel7.setBackground(new java.awt.Color(249, 249, 249));
        jPanel7.setBorder(javax.swing.BorderFactory.createMatteBorder(34, 14, 0, 14, new java.awt.Color(249, 249, 249)));
        jPanel7.setLayout(new java.awt.BorderLayout());

        jPanel1.setBackground(new java.awt.Color(240, 240, 240));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 102, 153), 2));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel5.setBackground(new java.awt.Color(238, 238, 239));
        jPanel5.setBorder(javax.swing.BorderFactory.createMatteBorder(4, 10, 10, 10, new java.awt.Color(238, 238, 239)));
        jPanel5.setLayout(new java.awt.BorderLayout());

        jPanel2.setBackground(new java.awt.Color(238, 238, 239));
        jPanel2.setLayout(new java.awt.GridLayout(4, 1, 0, 2));

        jLabel1.setFont(new java.awt.Font("Book Antiqua", 1, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(102, 102, 102));
        jLabel1.setText("Current Annotator Name:");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jPanel2.add(jLabel1);

        jPanel3.setBackground(new java.awt.Color(238, 238, 239));
        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        jPanel3.setLayout(new java.awt.BorderLayout());

        jUserName.setBackground(new java.awt.Color(255, 255, 204));
        jUserName.setFont(new java.awt.Font("Bookman Old Style", 0, 13)); // NOI18N
        jUserName.setBorder(null);
        jPanel3.add(jUserName, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel3);

        jLabel3.setFont(new java.awt.Font("Book Antiqua", 1, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(102, 102, 102));
        jLabel3.setText("Password ");
        jLabel3.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jPanel2.add(jLabel3);

        jPanel4.setBackground(new java.awt.Color(238, 238, 239));
        jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 204)));
        jPanel4.setLayout(new java.awt.BorderLayout());

        jPassword.setBackground(new java.awt.Color(255, 255, 204));
        jPassword.setFont(new java.awt.Font("Bookman Old Style", 0, 13)); // NOI18N
        jPassword.setText("jPasswordField1");
        jPassword.setBorder(null);
        jPanel4.add(jPassword, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel4);

        jPanel5.add(jPanel2, java.awt.BorderLayout.CENTER);

        jPanel6.setBackground(new java.awt.Color(238, 238, 239));

        jCheckBox_apply_toAll.setFont(new java.awt.Font("Bookman Old Style", 0, 12)); // NOI18N
        jCheckBox_apply_toAll.setForeground(new java.awt.Color(0, 102, 102));
        jCheckBox_apply_toAll.setText("Apply this User to all existing annotations.");
        jCheckBox_apply_toAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_apply_toAllActionPerformed(evt);
            }
        });

        jCheckBox_apply_toRelated.setFont(new java.awt.Font("Bookman Old Style", 0, 12)); // NOI18N
        jCheckBox_apply_toRelated.setForeground(new java.awt.Color(0, 102, 102));
        jCheckBox_apply_toRelated.setText("Apply changes to related annotations.");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox_apply_toRelated)
                    .addComponent(jCheckBox_apply_toAll))
                .addGap(0, 90, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(152, Short.MAX_VALUE)
                .addComponent(jCheckBox_apply_toRelated)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox_apply_toAll)
                .addContainerGap())
        );

        jPanel5.add(jPanel6, java.awt.BorderLayout.SOUTH);

        jPanel1.add(jPanel5, java.awt.BorderLayout.CENTER);

        jPanel7.add(jPanel1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel7, java.awt.BorderLayout.CENTER);

        jPanel8.setBackground(new java.awt.Color(251, 251, 251));
        jPanel8.setBorder(javax.swing.BorderFactory.createMatteBorder(8, 0, 1, 8, new java.awt.Color(251, 251, 251)));
        jPanel8.setLayout(new java.awt.BorderLayout());

        jPanel9.setBackground(new java.awt.Color(251, 251, 251));
        jPanel9.setLayout(new java.awt.GridLayout(1, 0));

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel9.add(jButton2);

        jButton1.setText("OK");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel9.add(jButton1);

        jPanel8.add(jPanel9, java.awt.BorderLayout.EAST);

        jID.setFont(new java.awt.Font("Book Antiqua", 1, 12)); // NOI18N
        jID.setForeground(new java.awt.Color(102, 102, 102));
        jID.setText("ID:");
        jID.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jPanel8.add(jID, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel8, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        exitProc();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowLostFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowLostFocus
        //exitProc();
    }//GEN-LAST:event_formWindowLostFocus

    private void jCheckBox_apply_toAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_apply_toAllActionPerformed
        jCheckBox_apply_toRelated.setEnabled(!jCheckBox_apply_toAll.isSelected());
    }//GEN-LAST:event_jCheckBox_apply_toAllActionPerformed

    /**exitProc process*/
    private void exitProc(){
        try{
            
            
            String text = this.jUserName.getText();
            String id = "01"; //this.jTextField2.getText();
            if (( text == null )||(id==null)){
                commons.Tools.beep();
                return;
            }else if (( text.trim().length() < 1)||( id.trim().length() < 1)) {
                commons.Tools.beep();
                return;
            }else{
                resultEditor.annotator.Manager.setCurrentAnnotator( text.trim() );
                //resultEditor.workSpace.WorkSet.password = this.jPassword.getPassword().toString();
                char[] pwdChars = this.jPassword.getPassword();
                resultEditor.workSpace.WorkSet.password = new String(pwdChars);
                //resultEditor.annotator.Manager.setCurrentAnnotatorID( id.trim() );
                __gui.updateScreen_for_variables();
                __gui.removeSelection();
                
            }

            __gui.config_saveProjectSetting();
            
            if( this.assignmentScreen == null ){                                                                   
                applyChanges(text, id, this.oldannotator_username, this.oldannotator_userid);
            }
            
            

        }catch(Exception ex){
        
        }
        config.system.SysConf.saveSystemConfigure();


        this.setVisible(false);
        __gui.removeSelection();
        
        if( this.assignmentScreen != null ){            
            assignmentScreen.sync();
        }
        
        this.dispose();
    }
    

    /**
     * By users' opinions got from checkboxes, apply change of current annotator
     * informations to existed annotations.
     */
    private void applyChanges(final String annotatorname_new, final String annotatorid_new,
            final String oldannotatorname, final String oldannotatorid){
        if(this.jCheckBox_apply_toAll.isSelected()){
            new Thread(){
                @Override
                public void run(){
                    resultEditor.annotations.Depot annotationdepot = new
                            resultEditor.annotations.Depot();
                    annotationdepot.changeAnnotator_toAll(annotatorname_new, annotatorid_new);
                }}.start();
            
        }else if(this.jCheckBox_apply_toRelated.isSelected()){
            new Thread(){
                @Override
                public void run(){
                    resultEditor.annotations.Depot annotationdepot = new
                            resultEditor.annotations.Depot();
                    annotationdepot.changeAnnotator_toRelated(annotatorname_new, annotatorid_new, oldannotatorname, oldannotatorid);
                }}.start();
        }
        resultEditor.workSpace.WorkSet.uid = null;
    }

    /**Show current annotator's name on this dialog*/
    private void updateScreen_for_CurrentAnnoator(){
        try{
            String currentannotator = resultEditor.annotator.Manager.getCurrentAnnotator();
            this.jUserName.setText(currentannotator);
            oldannotator_username = currentannotator;
            String currentannotatorid = resultEditor.annotator.Manager.getCurrentAnnotatorID();
            this.jTextField2.setText(currentannotatorid);
            oldannotator_userid = currentannotatorid;
            
            // list current password
            this.jPassword.setText( resultEditor.workSpace.WorkSet.password );
            
            
            // LIST ID
            this.jID.setText( "ID: " + resultEditor.workSpace.WorkSet.uid );
            
        }catch(Exception ex){

        }
    }

    /**Set the location at the middle of its father window.*/
    private void setLocation(){

        // set frame size of this dialog
        this.setPreferredSize(new Dimension(618,380));

        // valid check
        if( __gui == null )
            return;

        // calculate the location to set this dialog at the middle of parent
        // dialog
        int parentX = __gui.getX(),
            parentY = __gui.getY();
        int parentWidth = __gui.getWidth(),
            parentHeight = __gui.getHeight();

        int width = this.getWidth(), height = this.getHeight();
        int x = parentX + (int)(parentWidth  -  width)/2,
            y = parentY + (int)(parentHeight - height)/2;

        // set location of this dialog
        this.setLocation(x, y);
    }

    /**
    * @param args the command line arguments
    */
    /*public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ChangeAnnotator().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBox_apply_toAll;
    private javax.swing.JCheckBox jCheckBox_apply_toRelated;
    private javax.swing.JLabel jID;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JPasswordField jPassword;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jUserName;
    // End of variables declaration//GEN-END:variables

}
